package com.zthz.douxing

import grails.transaction.Transactional
import org.springframework.validation.FieldError

@Transactional
class ErrorMessageServicesService {
    def messageSource

    def getErrorMsg(cmdObject) {
        def errorMsg = ""
        if (cmdObject?.hasErrors()) {
            for (FieldError error : cmdObject.getErrors().getAllErrors()) {
                log.debug(error.toString())
                log.info(error.codes.toString())
                errorMsg += messageSource.getMessage(error, java.util.Locale.SIMPLIFIED_CHINESE)+","
            }
        }

        if(errorMsg.equals("")||errorMsg.size()==0)

            return errorMsg
        else

            return errorMsg.substring(0,errorMsg.length()-1)
    }

    def getErrorMsgByCode(String code){

        return messageSource.getMessage(code,null,java.util.Locale.SIMPLIFIED_CHINESE)

    }

}